package com.example.t1androidbm;

import java.io.IOException;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import com.example.t1androidbm.Activity_P1.ObtenerPreguntaBackground;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;

public class Activity_P2 extends Activity {

	private TextView txtResultadoServicio;
	private String url = "http://190.44.108.8:8080/WST1/services/EncuestaWService";
	private String NAMESPACE = "http://ws";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_activity__p1);
		txtResultadoServicio = (TextView) findViewById(R.id.txtPre1);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_activity__p1, menu);
		return true;
	}

	public void recibirPregunta(View v) {
		txtResultadoServicio.setText("");
		new ObtenerPreguntaBackground().execute();
	}

	public void ir(View v) {

	}

	class ObtenerPreguntaBackground extends AsyncTask<Void, Void, String> {

		@Override
		protected String doInBackground(Void... params) {

			String resultadoJSON = "";
			String METHOD_NAME = "getPreguntas";
			String SOAP_ACTION = NAMESPACE + "/" + METHOD_NAME;
			SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
			SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(
					SoapEnvelope.VER11);
			soapEnvelope.dotNet = true;
			soapEnvelope.implicitTypes = true;
			soapEnvelope.setOutputSoapObject(request);

			HttpTransportSE aht = new HttpTransportSE(url);
			Log.d("Cliente SOAP", "Iniciando Consulta");
			try {
				aht.call(SOAP_ACTION, soapEnvelope);
				SoapPrimitive resultString = (SoapPrimitive) soapEnvelope
						.getResponse();
				resultadoJSON = resultString.toString();
				Log.d("Cliente SOAP", "Finalizando Consulta");

			} catch (IOException e) {
				e.printStackTrace();
			} catch (XmlPullParserException e) {
				e.printStackTrace();
			}

			return resultadoJSON;
		}

		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
			try {
				String texto = "";
				JSONObject resultado = new JSONObject(result);
				String contenido = resultado.getString("Contenido");
				Log.d("ClienteSoap", contenido);
				JSONArray preguntasJSONArray = new JSONArray(contenido);

				texto += "Numero:\n"
						+ preguntasJSONArray.getJSONObject(1).getString("id")
						+ "\n";
				texto += preguntasJSONArray.getJSONObject(1).getString(
						"descripcion")
						+ "\n";

				txtResultadoServicio.setText(texto);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
	}
}
